import {Component, Input, OnInit} from '@angular/core';
import {Materiaincoming} from '../../../../model/materiaincoming';
import {AdminService} from "../../../../services/admin.service";
import {AlertService} from "../../../../services/alert.service";
import {AuthService} from "../../../../services/auth.service";

@Component({
  selector: 'app-materiaincomingedit',
  templateUrl: './materiaincomingedit.component.html',
  styleUrls: ['./materiaincomingedit.component.css']
})
export class MateriaincomingeditComponent implements OnInit {
  @Input() optype:string
  @Input() id:string

  m:Materiaincoming;
  GysArray:Array<any>;
  GgArray:Array<any>;
  CgyArray:Array<any>;

  PmArray:Array<any>;
  constructor( private adminService:AdminService,private alertService:AlertService,
               private authService:AuthService) {
    this.m=new Materiaincoming();
    this.GysArray=new Array();
    this.GgArray=new Array();
    this.CgyArray=new Array();

    this.PmArray=new Array();
  }

  ngOnInit(): void {
  this.getGgArray();
  this.geCgyArray();

  this.getGysArray();
  this.getPmArray();
    if(this.optype=="update"){
      this.getMaterainByid();
    }
  }

  getMaterainByid(){
    this.adminService.getMatincomingbyid(this.id).then((data:any)=>{
      if(data.msg == "ok"){
        this.m = data.obj
      }else {
        this.alertService.showError("根据id获取包材入库失败")
      }
    })
  }

  save(){
    this.adminService.saveMatincoming(this.m).then((data:any)=>{
      if(data.msg == "ok"){
        this.alertService.showSuccess("设置包材入库成功")
      }else {
        this.alertService.showError("设置包材入库失败")
      }
    })
  }

  getGgArray() {
    this.adminService.getReferdataSelectByTypeid("9823f6c4b1104982a9a5ca162efef9a3").then((data:any)=>{
      if(data.msg == "ok"){
        this.GgArray = data.list
      }else{
        this.alertService.showError("获取规格数组失败")
      }
    })
  }
  geCgyArray() {
    this.authService.getMemberListByRole("11988d2da3984556b02266ca1bdf14f8").then((data:any)=>{
      if(data.msg == "ok"){
        this.CgyArray = data.list
      }else{
        this.alertService.showError("获取采购人员数组失败")
      }
    })
  }


  getGysArray() {
    this.adminService.getGysSel().then((data:any)=>{
      if(data.msg == "ok"){
        this.GysArray = data.list
      }else{
        this.alertService.showError("获取供应商数组失败")
      }
    })
  }

  getPmArray() {
    this.adminService.getNzSelCateByTypeid("cd6a249e86004f0c9c2f4aa252de522d").then((data:any)=>{
      if(data.msg == "ok"){
        this.PmArray = data.list
      }else{
        this.alertService.showError("获取品名数组失败")
      }
    })
  }
}
